﻿using System;
using Cosmos.Compiler.Builder;
using Cosmos.Sys.FileSystem;
using Cosmos.Hardware;
using Cosmos.Sys;
using CosView.GUI;

namespace CosView
{
    class EProgram
    {
        /*#region Cosmos Builder logic
        // Most users wont touch this. This will call the Cosmos Build tool
        [STAThread]
        static void Main(string[] args)
        {
            BuildUI.Run();
        }
        #endregion*/

        // Main entry point of the kernel
        public static void Init()
        {
            var xBoot = new Cosmos.Sys.Boot();
            xBoot.Execute();

            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("View - OS");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("  Alpha 01");

            //Cosmos.Sys.VFSManager.Init();

            //Console.WriteLine("Welcome! You just booted C# code. Please edit Program.cs to fit your needs");
            try
            {
                /*Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                int loaded = 0;
                Load:
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();

                Console.WriteLine("Loading... [" + loaded.ToString() + "%]");
                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.Red;
                loaded++;
                for (int i = 0; i < loaded; i++)
                {
                    Console.Write('#');
                }
                System.Threading.Thread.Sleep(50);
                if (loaded < Console.WindowWidth) goto Load;
                else goto Cont;
                Cont:*/
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();
                Console.WriteLine("Loading... [OK]");
                System.Threading.Thread.Sleep(10);
                goto Main;
            Main:
                Console.Clear();
                string[] drvs = Cosmos.Sys.VFSManager.GetLogicalDrives();
                FilesystemEntry[] vols = Cosmos.Sys.VFSManager.GetVolumes();

                VGAScreen.SetMode720x480x16();

                Drawer d = new Drawer();
                Drawer.Init();

                DrawerObject Panel = new DrawerObject(Console.WindowWidth - 1, 5, 1, 1, ConsoleColor.Red, ConsoleColor.Red);
                DrawerObject Label = new DrawerObject(Console.WindowWidth - 2, 4, 2, 2, ConsoleColor.Blue, ConsoleColor.White);
                Label.Text = "A message here.";
                Drawer.AddObject(Panel);
                Drawer.AddObject(Label);

                DrawerObject TextObj = new DrawerObject(Console.WindowWidth - 1, 8, 1, 7, ConsoleColor.Green, ConsoleColor.Red);
                TextObj.Text = "MyTextHere";
                TextObj.UID = "MYTexxtBxox";

                Drawer.AddObject(TextObj);

                /*Form f = new Form();
                f.X = 4;
                f.Y = 4;
                f.Width = 5;
                f.Height = 5;*/

                foreach (string s in drvs)
                {
                    Console.WriteLine("[DRIVE] " + s);
                }
                foreach (FilesystemEntry e in vols)
                {
                    Console.WriteLine("[VOLUME] " + e.Name);
                }

            Loop:
                ConsoleColor cc = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("-> ");
                Console.ForegroundColor = cc;
                Console.BackgroundColor = ConsoleColor.Black;
                object[] l_cmds = Console.ReadLine().Split(' ');
                string l_cmd = l_cmds[0].ToString();
                switch (l_cmd)
                {
                    case "shutdown":
                        Deboot.ShutDown();
                        break;
                    case "reboot":
                        Deboot.Reboot();
                        break;
                    case "cls":
                        Drawer.Invalidate();
                        break;
                    case "ctx":
                        TextObj.Text = "";
                        for (int w = 1; w < l_cmds.Length; w++)
                        {
                            if (w != 1) TextObj.Text += " ";
                            TextObj.Text += l_cmds[w];
                        }
                        Drawer.Objects[2] = TextObj;
                        break;
                }
                goto Loop;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fail (" + ex.Message + ")...");
            }

            /*while (true)
                ;*/
        }
    }
}